package com.drinks.yiniang.adapter.user.wallet;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.user.wallet.RechargePackageJson;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 充值套餐列表adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class RechargePackageAdapter extends BaseQuickRCVAdapter<RechargePackageJson.RechargePackageData, BaseViewHolder> {

    private SpannableStringBuilder mBuilder;

    private int mSatrt1;
    private int mEnd1;
    private int mSatrt2;
    private int mEnd2;

    private int sp25;

    public RechargePackageAdapter(Context mContext, @Nullable List<RechargePackageJson.RechargePackageData> data) {
        super(R.layout.item_recharge_package, data);

        mBuilder = new SpannableStringBuilder();

        sp25 = ScreenSizeUtils.sp2px(25);
    }

    @Override
    protected void convert(BaseViewHolder helper, RechargePackageJson.RechargePackageData item) {
        mBuilder.clearSpans();
        mBuilder.clear();

        mBuilder.append("充");
        mSatrt1 = mBuilder.length();
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getPrice()));
        mEnd1 = mBuilder.length();
        mBuilder.append("元   送");
        mSatrt2 = mBuilder.length();
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getGain_point()));
        mEnd2 = mBuilder.length();
        mBuilder.append("积分");

        mBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.color_main)),mSatrt1, mEnd1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBuilder.setSpan(new AbsoluteSizeSpan(sp25), mSatrt1, mEnd1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.color_main)),mSatrt2, mEnd2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBuilder.setSpan(new AbsoluteSizeSpan(sp25), mSatrt2, mEnd2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        helper.setText(R.id.tv_recharge_pkg_money, mBuilder);

        if(item.getCoupon() != null) {
            mBuilder.clearSpans();
            mBuilder.clear();
            mBuilder.append("得");
            mBuilder.append(StringUtils.getNullEmptyConvert__(item.getCoupon().getAmount()));
            mBuilder.append("元优惠券");
            helper.setText(R.id.tv_recharge_pkg_coupon, mBuilder);
        } else {
            helper.setText(R.id.tv_recharge_pkg_coupon, "得--元优惠券");
        }
    }
}
